package io.renren.modules.workmanagement.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;
import io.renren.modules.workmanagement.constans.UserConstans;
import io.renren.modules.workmanagement.dao.CourseDao;
import io.renren.modules.workmanagement.entity.ClassCourseEntity;
import io.renren.modules.workmanagement.entity.CourseEntity;
import io.renren.modules.workmanagement.entity.DepartmentEntity;
import io.renren.modules.workmanagement.service.ClassCourseService;
import io.renren.modules.workmanagement.service.CourseService;
import io.renren.modules.workmanagement.service.DepartmentService;
import io.renren.modules.workmanagement.utils.PojoUtils;
import io.renren.modules.workmanagement.vo.ClassCourse.CourseCourseVO;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.Set;


@Service("courseService")
public class CourseServiceImpl extends ServiceImpl<CourseDao, CourseEntity> implements CourseService {
    @Autowired
    private DepartmentService departmentService;
    @Autowired
    private ClassCourseService classCourseService;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        Long userId = (Long)params.get(UserConstans.USER_ID_KEY);
        LambdaQueryWrapper<CourseEntity> wrapper = new LambdaQueryWrapper<>();
        String key = (String) params.get("key");
        if(StringUtils.isNotBlank(key)){
            wrapper.like(CourseEntity::getName,"%"+key+"%");
        }
        DepartmentEntity department = departmentService.getByAdminId(userId);
        IPage<CourseEntity> page = this.page(
                new Query<CourseEntity>().getPage(params),
                wrapper.eq(CourseEntity::getDepartmentId,department.getId())
        );

        return new PageUtils(page);
    }

    @Override
    public void saveCourse(CourseEntity course, Long userId) {
        DepartmentEntity department = departmentService.getByAdminId(userId);
        course.setDepartmentId(department.getId());
        this.save(course);
    }

    @Override
    public List<CourseEntity> getByDepartmentId(Long id) {
        return this.list(new LambdaQueryWrapper<CourseEntity>().eq(CourseEntity::getDepartmentId,id));
    }

    @Override
    public List<CourseCourseVO> getMyCourse(Long userId) {
        List<ClassCourseEntity> classCourses = classCourseService.getByTeacherId(userId);
        Set<Long> courseIds = PojoUtils.streamMapSet(classCourses, ClassCourseEntity::getCourseId);
        List<CourseEntity> courseEntityList = this.listByIds(courseIds);
        return PojoUtils.streamMapList(courseEntityList, this::CourseConvert);
    }

    @Override
    public List<CourseEntity> likeByName(String key) {
        return this.lambdaQuery().like(CourseEntity::getName, "%" + key + "%").list();
    }

    private CourseCourseVO CourseConvert(CourseEntity courseEntity){
        CourseCourseVO courseCourseVO = new CourseCourseVO();
        courseCourseVO.setCourseId(courseEntity.getId());
        courseCourseVO.setCourseName(courseEntity.getName());
        return courseCourseVO;
    }

}
